package com.hitearth.ccbrower;

import android.app.Activity;


import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.*;
import android.widget.LinearLayout;

import com.hitearth.ccbrower.webview.*;

public class MainActivity extends Activity implements View.OnKeyListener, View.OnTouchListener {

    public WebView webView;
    public UrlText text1;
    public LinearLayout linearLayout1;
    public String storeURL;
    private int x1, x2;
    private int y1, y2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
        text1 = (UrlText) findViewById(R.id.urlText1);

        text1.setOnKeyListener(this);

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setOnTouchListener(this);
        webView.setWebViewClient(new HybridWebViewClient(this));
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!isEmpty2(storeURL) && !storeURL.equals(webView.getUrl())) {
            webView.loadUrl(storeURL);
            return;
        }
        if (isEmpty2(storeURL) && isEmpty2(webView.getUrl())) {
            webView.loadUrl("file:///android_asset/home.html");
        }
    }

    private boolean isEmpty2(String s) {
        if (s == null) return true;
        return s.isEmpty();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("url", webView.getUrl());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        storeURL = savedInstanceState.getString("url");
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            String url = text1.getText().toString();
            if (url == null || url.isEmpty()) {
                webView.loadUrl("file:///android_asset/home.html");
                return true;
            }
            if (!url.startsWith("http://") && !url.startsWith("https://")) {
                url = "http://" + url;
            }
            webView.loadUrl(url);
            return true;
        } else
            return false;
    }

    @Override
    public boolean onTouch(View view, MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = (int) e.getX();
                y1 = (int) e.getY();
                break;

            case MotionEvent.ACTION_UP:
                x2 = (int) e.getX();
                y2 = (int) e.getY();
                if (y1 - y2 > 50) {//向上滑
                    linearLayout1.setVisibility(View.GONE);

                } else if (y2 - y1 > 500) {
                    linearLayout1.setVisibility(View.VISIBLE);
                }
                break;
        }
        return false;
    }
}
